<?php
// +----------------------------------------------------------------------
// * Created by PhpStorm.
// +----------------------------------------------------------------------
// * Author: D H
// +----------------------------------------------------------------------
// * Date: 2017/9/8
// +----------------------------------------------------------------------
// * Time: 17:05
// +----------------------------------------------------------------------
namespace app\api\logic;
use think\console\command\make\Controller;
use think\Request;
use tool\Common;
use think\Db;

class Show extends Base{
    //+---------------------------------------------------
    //*首页数据
    //+---------------------------------------------------
    //得到分类列表(首页展示)
    public function getCatList(){
        $list = Db::name('cat')->where('parentID',0)->field(['id' => 'catID','name' => 'catName'])->select();
        return Common::rm(1,'操作成功',$list);
    }
    //得到热销供应商列表
    public function getHotProductList(){
        $list = Db::view('product_hot','productID')
            ->view('product','sellerID','product_hot.productID = product.id')
            ->view('product_content',['titleImage'],'product.id = product_content.productID')
            ->view('user',['sellerName','logo'],'product.sellerID = user.id')
            ->select();
        $total = count($list);
        foreach ($list as $k => $val){
            $list[$k]['titleImage'] = json_decode($val['titleImage'],true);
            $list[$k]['titleImage'] = $list[$k]['titleImage'][0];
        }
       /* foreach ($list as $k => $val){
            $list[$k]['titleImage'] = $list[$k]['titleImage'][0];
        }*/
        $newData = [];
        foreach ($list as $key => $val) {
            $newData[$val['sellerID']][] = $val;
        }
        $arr = [];
        foreach ($newData as $key => $val) {
            $arr[$key]['productID'] = $val[0]['productID'];
            $arr[$key]['sellerID'] = $val[0]['sellerID'];
            $arr[$key]['sellerName'] = $val[0]['sellerName'];
            $arr[$key]['logo'] = $val[0]['logo'];
            foreach ($val as $_key => $_val) {
                $arr[$key]['titleImage'][$_key] = $_val['titleImage'];
            }
        }
        $arr = array_reverse($arr);
        return Common::rm(1,'操作成功',$arr);
    }
    //得到精选市场
    public function getSelectProductList($data){
        //得到精选分类列表
        $catList['name'] = '灭火器';
        $catList['catList'] = Db::name('cat')->where('parentID',$data)
            ->field(['id' => 'catID','name' => 'catName'])
            ->select();
        $id = [];
        foreach ($catList['catList'] as $k => $val){
          $id[$k] = $val['catID'];
        }
        $productList = Db::view('product',['name','marketsPrice','brandID','id' => 'productID'])
            ->view('brand',['name' => 'brandName'],'product.brandID = brand.id')
            ->view('product_content','titleImage','product.id = product_content.productID')
            ->where('catalogID','in',$id)
            ->limit(6)
            ->select();
        $brand = [];
        foreach ($productList as $k => $val){
            $productList[$k]['titleImage'] = json_decode($val['titleImage']);
            $productList[$k]['productID'] = $productList[$k]['productID'];
            $productList[$k]['titleImage'] = $productList[$k]['titleImage'][0];
            $productList[$k]['name'] = $productList[$k]['brandName'] .'/'. $productList[$k]['name'];
            //$brand[$k]['brandID'] = $productList[$k]['brandID'];
            //$brand[$k]['brandName'] = $productList[$k]['brandName'];
            unset($productList[$k]['brandID']);
            unset($productList[$k]['brandName']);
        }
        return Common::rm(1,'操作成功',['cat' => $catList, 'product' => $productList]);
    }
    //+---------------------------------------------------
    //*分类数据
    //+---------------------------------------------------
    //根据分类得到产品列表列表 --顶级分类点击
    public function topCat($data){
        //商品类目
        $catList = Db::name('cat')->field(['id' => 'catID','name' => 'catName'])
            ->where('parentID',$data['catID'])
            ->select();
        $catID = [];
        foreach ($catList as $k => $val){
            $catID[$k]= $val['catID'];
        }
        //得到商品列表
        $pList = Db::view('product',['name' => 'productName','marketsPrice'])
            ->view('user','sellerName','product.sellerID = user.id')
            ->view('brand',['id' => 'brandID','name' => 'brandName'],'product.brandID = brand.id')
            ->view('product_content','titleImage','product_content.productID = product.id   ')
            ->where('product.catalogID','in',$catID)
            ->limit(($data['page'] -1)*15,15)
            ->select();
        //得到商品总数
        $total = Db::view('product',['name' => 'productName','marketsPrice'])
            ->view('user','sellerName','product.sellerID = user.id')
            ->view('brand',['id' => 'brandID','name' => 'brandName'],'product.brandID = brand.id')
            ->view('product_content','titleImage','product_content.productID = product.id   ')
            ->where('product.catalogID','in',$catID)
            ->select();
        $proList = [];
        $proIDS = [];
        //产品列表
        foreach ($pList as $k => $val){
            $proList[$k]['titleImage'] = json_decode($val['titleImage']);
            $proList[$k]['titleImage'] = $proList[$k]['titleImage'][0];
            $proList[$k]['name'] = $val['brandName'].'/'.$val['productName'];
            $proList[$k]['sellerName'] = $val['sellerName'];
            $proList[$k]['marketsPrice'] = $val['marketsPrice'];
            //产品ID
            $proIDS[$k] = $val['brandID'];
        }
        //品牌列表
        $brandList = Db::name('brand')
            ->field(['id' => 'brandID','name' => 'brandName'])
            ->where('id','in',$proIDS)
            ->distinct(true)
            ->select();
        return Common::rm(1,'操作成功',[
            'total' => count($total),
            'catList' => $catList,
            'brand' => $brandList,
            'productList' => $proList
        ]);

    }
    //根据分类得到产品列表列表  --二级分类点击
    public function nextCat($data){
        //得到商品列表
        $pList = Db::view('product',['name' => 'productName','marketsPrice'])
            ->view('user','sellerName','product.sellerID = user.id')
            ->view('brand',['id' => 'brandID','name' => 'brandName'],'product.brandID = brand.id')
            ->view('product_content','titleImage','product_content.productID = product.id   ')
            ->where('product.catalogID',$data['catID'])
            ->limit(($data['page'] -1)*15,15)
            ->select();
        $proList = [];
        $proIDS = [];
        //产品列表
        foreach ($pList as $k => $val){
            $proList[$k]['titleImage'] = json_decode($val['titleImage']);
            $proList[$k]['titleImage'] = $proList[$k]['titleImage'][0];
            $proList[$k]['name'] = $val['brandName'].'/'.$val['productName'];
            $proList[$k]['sellerName'] = $val['sellerName'];
            $proList[$k]['marketsPrice'] = $val['marketsPrice'];
            //产品ID
            $proIDS[$k] = $val['brandID'];
        }
        //品牌列表
        $brandList = Db::name('brand')
            ->field(['id' => 'brandID','name' => 'brandName'])
            ->where('id','in',$proIDS)
            ->distinct(true)
            ->select();
        return Common::rm(1,'操作成功',[
            'brand' => $brandList,
            'productList' => $proList
        ]);
    }
    //+---------------------------------------------------
    //*页面返回
    //+---------------------------------------------------
    //首页列表
    public function index(){
        $catList =  $this->getCatList();
        $hotProduct = $this->getHotProductList();
        $selectProduct_1 = $this->getSelectProductList(1);
        $selectProduct_2 = $this->getSelectProductList(2);
        $selectProduct_3 = $this->getSelectProductList(3);
        $index = [
            'catList' => $catList['content'],
            'hotProductList' =>$hotProduct,
            'selectProductList' => [
                $selectProduct_1['content'],
                $selectProduct_2['content'],
                $selectProduct_3['content'],
            ]
        ];
        return Common::rm(1,'操作成功',$index);
    }
    //产品分类页
    public function productCat(){
        //$data = $this->data;
        $data = [
            'catID' => 1,
            'page' => 1
        ];
        $parID = Db::name('cat')->field('parentID')->where('id',$data['catID'])->find();
        if ($parID['parentID'] == 0){//这个是顶级分类
           return $this->topCat($data);
        }else{//这个是二级分类
            return $this->nextCat($data);
        }
    }
    //产品分类页---分类及品牌筛选
    public function screeningProductByID(){
        $data = $this->data;
        $data = [
            'catID' =>1,
            'brand' => 1,
            'page' => 1
        ];
        //分页总数
        $t_sql = Db::view('product',['name' => 'productName','marketsPrice'])
            ->view('user','sellerName','product.sellerID = user.id')
            ->view('brand',['id' => 'brandID','name' => 'brandName'],'product.brandID = brand.id')
            ->view('product_content','titleImage','product_content.productID = product.id');
        $sql = Db::view('product',['name' => 'productName','marketsPrice'])
            ->view('user','sellerName','product.sellerID = user.id')
            ->view('brand',['id' => 'brandID','name' => 'brandName'],'product.brandID = brand.id')
            ->view('product_content','titleImage','product_content.productID = product.id')
            ->limit(($data['page'] -1)*15,15);
       if (count($data) > 2){
          //得到商品列表
        $pList = $sql->where('product.brandID',$data['brand'])
            ->where('product.catalogID',$data['catID'])
            ->select();
           $t_pList = $t_sql->where('product.brandID',$data['brand'])
           ->where('product.catalogID',$data['catID'])
           ->select();
           $total = count($t_pList);
       }else{
           foreach ($data as $k => $val){
               if ($k == 'catID'){
                   $pList = $sql->where('product.catalogID',$data['catID'])->select();
                   $t_pList = $t_sql->where('product.catalogID',$data['catID'])->select();
                   $total = count($t_pList);
                   break;
               }else{
                   $pList = $sql->where('product.brandID',$data['brand'])->select();
                   $t_pList = $t_sql->where('product.brandID',$data['brand'])->select();
                   $total = count($t_pList);
               }
           }
       }
        $proList = [];
        //产品列表
        foreach ($pList as $k => $val){
            $proList[$k]['titleImage'] = json_decode($val['titleImage']);
            $proList[$k]['titleImage'] = $proList[$k]['titleImage'][0];
            $proList[$k]['name'] = $val['brandName'].'/'.$val['productName'];
            $proList[$k]['sellerName'] = $val['sellerName'];
            $proList[$k]['marketsPrice'] = $val['marketsPrice'];
        }
        return Common::rm(1,'操作成功',[
            'total' => $total,
            'productList' => $proList
        ]);
    }
    //产品分类页---价格筛选

    //产品详情页
    public function productDetail(){
        $data = [
            'productID' => 1
        ];
        $list = Db::view('product',['id' => 'productID','name' => 'productName','marketsPrice','protocolPrice','score'])
            ->view('product_content','details,titleImage','product.id = product_content.productID')
            ->view('user',['sellerName','logo','address','postcode','tel','fax','mobile','contactman'],'user.id = product.sellerID')
            ->where('product.id',$data['productID'])
            ->find();
        $list['discount'] = (round((1-($list['protocolPrice']/$list['marketsPrice']))*100)).'%';
        $list['titleImage'] = json_decode($list['titleImage'],true);
        $list['details'] = json_decode($list['details'],true);
        //公司信息
        $company = [
            'sellerName' => $list['sellerName'],
            'logo' => $list['logo'],
            'address' => $list['address'],
            'postcode' => $list['postcode'],
            'tel' => $list['tel'],
            'fax' => $list['fax'],
            'mobile' => $list['mobile'],
            'contactman' => $list['contactman']
        ];
        //产品信息
        $productList = [
            'productID' => $list['productID'],
            'productName' => $list['productName'],
            'marketsPrice' => $list['marketsPrice'],
            'protocolPrice' => $list['protocolPrice'],
            'discount' => $list['discount'],
            'score' => $list['score'],
            'titleImage' => $list['titleImage'],
            'details' => $list['details'],
            'company' => $company
        ];
        //产品信息
        return Common::rm(1,'操作成功',$productList);
    }

}